<template>
  <div class="u-p-30">
    <el-form
      ref="dataForm"
      v-loading="loadingFlag"
      style="width: 50%"
      :model="dataForm"
      :rules="dataRule"
      size="small"
      label-width="80px"
    >
      <el-form-item label="账号">
        <span>{{ dataForm.username }}</span>
      </el-form-item>
      <el-form-item label="原密码" prop="password">
        <el-input v-model="dataForm.password" type="password" />
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="dataForm.newPassword" type="password" />
      </el-form-item>
      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input v-model="dataForm.confirmPassword" type="password" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getInfo, saveInfoPassword } from '@/api/user'
export default {
  data() {
    var validateConfirmPassword = (rule, value, callback) => {
      if (this.dataForm.newPassword !== value) {
        callback(new Error('确认密码与新密码不一致'))
      } else {
        callback()
      }
    }
    return {
      type: 2,
      submitLoading: false,
      dataForm: {
        username: '',
        oldPassword: '',
        newPassword: ''
      },
      dataRule: {
        password: [
          { required: true, message: '原密码不能为空', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '新密码不能为空', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { validator: validateConfirmPassword, trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getUserInfo()
  },
  methods: {
    // 获取当前管理员信息
    getUserInfo() {
      getInfo().then((response) => {
        if (response && response.code === 1) {
          this.dataForm.username = response.data.username
        } else {
          this.userInfo = []
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const data = {
            oldPassword: this.dataForm.password,
            newPassword: this.dataForm.newPassword
          }
          saveInfoPassword(data).then((response) => {
            if (response && response.code === 1) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 50,
                onClose: () => {
                  this.submitLoading = false
                }
              })
            } else {
              this.submitLoading = false
              this.$message.error(response.msg)
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.leftBox {
  width: 200px;
  height: 100%;
  border-right: 3px solid #eee;
  .item {
    margin-right: -3px;
    height: 50px;
    line-height: 50px;
    font-size: 16px;
    color: #333;
    box-sizing: border-box;
    padding-left: 20px;
    cursor: pointer;
  }
  .itemActive {
    background: #e7f7ff;
    color: #0091ff;
    border-right: 3px solid #0091ff;
  }
}
.rightBox {
  flex: 1;
  padding-left: 30px;
}
</style>
